import React, {PureComponent} from "react";
import {Divider, Icon, notification, Popconfirm, Table} from "antd";
import {connect} from "react-redux";
import {tableProps} from "../../lib/ui";
import {del, query, showCreate, showModify, copyModel} from "./actions";
import {containerOrderList} from "../home/actions";

class List extends PureComponent {

    handleModify = record => {
        const {dispatch} = this.props;
        dispatch(showModify(record))
    };
    handleDeleteRole = id => {
        const {dispatch} = this.props;
        dispatch(del(id)).then(action => {
            if (action.error !== true) {
                notification.success({
                    message: '删除成功'
                });
                dispatch(query());
            } else {
                notification.error({
                    message: '删除失败:' + action.message
                });
            }
        });
    };

    newRole = record =>{
        const {dispatch} = this.props;
        const newData = {...record};
        delete newData.id;
        delete newData.name;
        dispatch(showCreate());
        dispatch(copyModel({...newData}));
    };

    componentWillMount() {
        const {dispatch} = this.props;
        dispatch(query());
    };

    render() {
        const columns = [
            {
                title: "角色名称",
                dataIndex: "name",
                width: "150px",
                render: (text, record) => {
                    return (record.name !== "超级系统管理员" && record.name !== "系统管理员" && record.name !== "机构管理员") ? <a
                        onClick={() => {
                            this.handleModify(record);
                        }}
                    >
                        {text}
                    </a>: text
                }
            }, {
                title: "菜单权限",
                dataIndex: "permissions",
                ellipsis: true,
                width: "250px"
            }, {
                title: "通知",
                dataIndex: "notice",
                width: "150px",
                render: text => (text === true ? "接收" : "关闭")
            }, {
                title: "授予权限",
                dataIndex: "equalAdmin",
                width: "150px",
                render: text => (text === true ? "开启" : "关闭")
            }, {
                title: "首页栏目权限",
                dataIndex: "canCheckData",
                ellipsis: true,
                width: "200px"
            }, {
                title: '功能',
                dataIndex: '',
                key: 'x',
                width: "100px",
                align: 'center',
                fixed: 'right',
                render: (text, record) => {
                    return (record.name !== "超级系统管理员" && record.name !== "系统管理员" && record.name !== "机构管理员") && <span>
                    <a title="复制当前角色信息并打开新建窗口" onClick={() => this.newRole(record)}><Icon type="copy"/></a>
                    <Divider type={"vertical"}/>
                    <Popconfirm title="确定删除此角色?" okText="是" cancelText="否"
                                onConfirm={() => this.handleDeleteRole(record.id)}>
                        <Icon type="delete"/>
                    </Popconfirm>
                </span>
                }
            }
        ];
        const {
            dataSource,
            loading
        } = this.props;
        // 初始化数据
        const newData = [];
        dataSource.forEach(data=>{
            if("超级系统管理员" === data.name || "系统管理员" === data.name){
                data.permissions = '默认所有';
                data.canCheckData = '默认所有';
                data.notice = true;
            }
            if("系统管理员" === data.name){
                data.name = '机构管理员';
            }
            newData.push(data);
        });
        return (
            <Table
                {...tableProps}
                columns={columns}
                dataSource={newData}
                loading={loading}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.role.list
    };
};

export default connect(mapStateToProps)(List);
